package service

import (
	"context"
	"strconv"
	__ "user_service/basic/proto"
	"user_service/model"
)

type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) AddPack(_ context.Context, in *__.AddPackReq) (*__.AddPackResp, error) {

	var p model.Package

	p = model.Package{
		Id:          strconv.FormatInt(in.Id, 10),
		Weight:      in.Weight,
		Destination: in.Destination,
		Urgent:      uint(in.Urgent),
	}

	p.Create()

	return &__.AddPackResp{
		Code: 200,
		Msg:  "添加成功",
	}, nil
}

func (s *Server) ListPack(_ context.Context, in *__.ListPackReq) (*__.ListPackResp, error) {

	var p model.Package

	list, err := p.List()
	if err != nil {
		return nil, err
	}

	var art []*__.ListPack

	for _, m := range list {
		lis := &__.ListPack{
			Id:          m.Id,
			Weight:      m.Weight,
			Destination: m.Destination,
			Urgent:      int64(m.Urgent),
		}

		art = append(art, lis)
	}

	return &__.ListPackResp{
		List: art,
	}, nil

}

func (s *Server) InfoPack(_ context.Context, in *__.InfoPackReq) (*__.InfoPackResp, error) {
	var p model.Package

	p.Info(in.Destination)

	return &__.InfoPackResp{
		Id:          p.Id,
		Weight:      p.Weight,
		Destination: p.Destination,
		Urgent:      int64(p.Urgent),
	}, nil
}

func (s *Server) Info(_ context.Context, in *__.InfoReq) (*__.InfoResp, error) {
	var p model.Package

	p.Infos(uint(in.Urgent))

	return &__.InfoResp{
		Id:          p.Id,
		Weight:      p.Weight,
		Destination: p.Destination,
		Urgent:      int64(p.Urgent),
	}, nil
}
